﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;

namespace ListConsoleTest
{
    class Program
    {
        private static FileStream _fileStream;
        static void Main(string[] args)
        {
            // int [] list = new int[7] {1,2,3,4,5,6,88 };
            // IntPtr intPtr = Marshal.AllocHGlobal(list.Length * sizeof(int));
            // Marshal.Copy(list, 0, intPtr, list.Length);
            // var stretch = new stretch { Id = 1, Number = 2, Numbers = intPtr };
            //// Marshal.FreeHGlobal(intPtr);
            // IntPtr totalPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(stretch)));
            // Marshal.StructureToPtr(stretch, totalPtr, true);

            // var saveData = IntPtrToByte<stretch>(totalPtr);
            //// Marshal.FreeHGlobal(totalPtr);


            // var resultPtr = ByteToIntPtr(saveData);
            // var newStretch = (stretch)Marshal.PtrToStructure(resultPtr, typeof(stretch));
            //// Marshal.FreeHGlobal(resultPtr);
            // var numbersPtr = newStretch.Numbers;
            // var result = new List<int>();
            // for (int i = 0; i < list.Length; i++)
            // {
            //     var data = (int)Marshal.PtrToStructure(numbersPtr + i * sizeof(int), typeof(int));
            //     result.Add(data);
            // }
            //var pointList = new List<Point>() {new Point {X = 1, Y = 2}, new Point {X = 3, Y = 4}};
            //byte[] data =new byte[pointList.Count*Marshal.SizeOf(typeof(Point))];
            //Buffer.BlockCopy(pointList.ToArray(), 0, data, 0, data.Length);
            try
            {
                var path = "D:\\test.json";
                //if (!File.Exists(path))
                //    throw new FileNotFoundException(nameof(path));
                using (_fileStream = new FileStream(path, FileMode.Open))
                {
                    throw new Exception();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            

            int a = 12_23;
            var mypath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"../Sequence");
            var ss = Path.GetFullPath("../ss.txt");
            Console.ReadLine();
        }

        private static byte[] IntPtrToByte<T>(IntPtr intPtr)
        {
            if (intPtr == IntPtr.Zero)
                throw new ArgumentNullException(nameof(intPtr));

            int length = Marshal.SizeOf(typeof(T));
            var data = new byte[length];
            Marshal.Copy(intPtr, data, 0, length);
            return data;
        }

        private static IntPtr ByteToIntPtr(byte[] data)
        {
            IntPtr intPtr = Marshal.AllocHGlobal(data.Length);
            Marshal.Copy(data, 0, intPtr, data.Length);
            return intPtr;
        }
    }

   public struct stretch{
        public int Id{ get; set; }
        public int Number { get; set; }
        public IntPtr Numbers { get; set; }
    }
}
